//
//  File.swift
//  Line
//
//  Created by admin on 2022/4/25.
//

import Foundation
import SceneKit

class RoadLogic {
    var selectWrongRoad = false
    let passOneJoint = ["joint_01","joint_02","joint_03","joint_04","joint_05"]
    let passTwoJoint = ["joint_01","joint_02","joint_03","joint_04","joint_05","joint_06","joint_07"]
    let passThreeJoint = ["joint_01","joint_2","joint5", "joint_03","joint_04", "joint_05", "joint_06", "joint_07","joint_08", "joint_09", "joint3", "joint6", "joint4"]
    var isRight = false
    var roadName = ["road_1","road_2","road_3","road_4","road_5","road_6","road_7","road_8","road_9","road_10","road_11","road_12","road_13","road_14","road_15","road_16","road_17","road_18","road_19","road_20","road_21","road_22","road_23","road_24","road_25","road_26","road_27","road_28","road_29","road_30"]
    var PassOnewRightRoad = ["Start","joint_01","road_1","road_2","road_3","joint_02","road_4","road_5","road_6","joint_03","road_7","road_8","road_9","joint_04","road_10","road_11","road_12","joint_05"]
    var PassTwoRightRoad = ["Start", "road_1", "road_2", "road_3", "road_4", "road_5", "road_6", "road_7", "road_8", "road_9", "road_10", "road_11", "road_12", "road_13", "road_14", "road_15", "Start_01"]
    var PassThreeRightRoad = [" Start", "road_1", "road_2", "road_3", "road_4", "road_5", "road_7", "road_8", "road_9","road_10", "road_11",  "road_12", "road_13", "road_14", "road_15", "road_16", "road_17", "road_18", "road_19", "road_20", "road_21", "road_22", "road_23", "road_24", "road_26", "road_25", "road_27", "Start"]
    var jointName = ["joint"]
    var logicDictionariesOne = [String:[String]]()
    var logicDictionariesTwo = [String:[String]]()
    var logicDictionariesThree = [String:[String]]()
    var isturn: Bool = false
    func turnOrNot(pass:Int, isInflectionPoint: String, node: SCNNode) {
        
        switch pass {
        case 1:
            
            let jointNode = node.childNode(withName: "joint", recursively: true)
            
            switch isInflectionPoint {
            case "Start_02":
                print("joint")
                jointNode?.childNode(withName: "joint_01", recursively: false)?.isHidden = false
            case"road_1":
                node.childNode(withName: "edge_1", recursively: false)?.isHidden = true
                node.childNode(withName: "edge_101", recursively: false)?.isHidden = true
                
            case"road_6":
                node.childNode(withName: "edge_6", recursively: false)?.isHidden = true
                node.childNode(withName: "edge_66", recursively: false)?.isHidden = true
                jointNode?.childNode(withName: "joint_03", recursively: false)?.isHidden = false
                
                print("edageTwo is hidden")
            case"road_9":
                node.childNode(withName: "edge_9", recursively: false)?.isHidden = true
                node.childNode(withName: "edge_99", recursively: false)?.isHidden = true
                jointNode?.childNode(withName: "joint_04", recursively: false)?.isHidden = false
            case"road_12":
                node.childNode(withName: "edge_12", recursively: false)?.isHidden = true
                node.childNode(withName: "edge_112", recursively: false)?.isHidden = true
                jointNode?.childNode(withName: "joint_05", recursively: false)?.isHidden = false
                canTap = false
            case"road_3":
                node.childNode(withName: "edge_3", recursively: false)?.isHidden = true
                node.childNode(withName: "edge_33", recursively: false)?.isHidden = true
                jointNode?.childNode(withName: "joint_02", recursively: false)?.isHidden = false
                print("edageOne? is hidden")
           
            default:
                break
            }
            
            
            if logicDictionariesOne.keys.contains(isInflectionPoint) {
                
                node.childNode(withName: (logicDictionariesOne[isInflectionPoint]?[0])!, recursively: true)?.isHidden = true
                node.childNode(withName: (logicDictionariesOne[isInflectionPoint]?[1])!, recursively: true)?.isHidden = true
                
                
                print("XXXXXXXXXXX____\((logicDictionariesOne[isInflectionPoint]?[0])!)___\((logicDictionariesOne[isInflectionPoint]?[1])!)____XXXXXXXXXX")
                
            }
            
            
        case 2:
            
            let jointNode = node.childNode(withName: "joint", recursively: true)
            var selectRoad = false
                      
            switch isInflectionPoint {
            case "road_1" :
                jointNode?.childNode(withName: "joint_01", recursively: true)?.isHidden = false
                jointNode?.childNode(withName: "joint_01", recursively: true)?.geometry?.materials.first?.diffuse.contents = "Start.PNG"
            case "road_4":
                jointNode?.childNode(withName: "joint_02", recursively: true)?.isHidden = false
                jointNode?.childNode(withName: "joint_02", recursively: true)?.geometry?.materials.first?.diffuse.contents = "Start.PNG"
            case "road_7":
                jointNode?.childNode(withName: "joint_03", recursively: true)?.isHidden = false
                jointNode?.childNode(withName: "joint_03", recursively: true)?.geometry?.materials.first?.diffuse.contents = "Start.PNG"
            case "road_10":
                jointNode?.childNode(withName: "joint_04", recursively: true)?.isHidden = false
                jointNode?.childNode(withName: "joint_04", recursively: true)?.geometry?.materials.first?.diffuse.contents = "Start.PNG"
            case "road_13":
                jointNode?.childNode(withName: "joint_05", recursively: true)?.isHidden = false
                jointNode?.childNode(withName: "joint_05", recursively: true)?.geometry?.materials.first?.diffuse.contents = "Start.PNG"
            case "road_14":
                if selectWrongRoad == true {
                    node.childNode(withName: "road_17", recursively: true)?.isHidden = false
                    node.childNode(withName: "edge_13", recursively: true)?.isHidden = true
                    node.childNode(withName: "road_13", recursively: true)?.isHidden = true
                    node.childNode(withName: "edge_113", recursively: true)?.isHidden = true
                }else {
                    node.childNode(withName: "road_17", recursively: true)?.isHidden = true
                }
            case "road_17":
                jointNode?.childNode(withName: "joint_07", recursively: true)?.isHidden = false
                jointNode?.childNode(withName: "joint_07", recursively: true)?.geometry?.materials.first?.diffuse.contents = "Start.PNG"
            case "Start_01":
                jointNode?.childNode(withName: "joint_06", recursively: true)?.isHidden = false
                jointNode?.childNode(withName: "joint_06", recursively: true)?.geometry?.materials.first?.diffuse.contents = "Start.PNG"
            case "road_16":
                node.childNode(withName: "edge_6", recursively: false)?.isHidden = true
                node.childNode(withName: "road_6", recursively: false)?.isHidden = true
              selectWrongRoad = true

            default:
                print("XXXX")
            }
                        
            if logicDictionariesOne.keys.contains(isInflectionPoint) {
                
                
                node.childNode(withName: (logicDictionariesOne[isInflectionPoint]?[0])!, recursively: true)?.isHidden = true
                node.childNode(withName: (logicDictionariesOne[isInflectionPoint]?[1])!, recursively: true)?.isHidden = true
                
                
                print("XXXXXXXXXXX____\((logicDictionariesOne[isInflectionPoint]?[0])!)___\((logicDictionariesOne[isInflectionPoint]?[1])!)____XXXXXXXXXX")
                
            }
            
           
            
            if isInflectionPoint == "road_16" {
                selectWrongRoad = true
                
            }
            
        case 3:
            
            let jointNode = node.childNode(withName: "joint", recursively: true)
            let jointDictionariereThree = [" road_1":["joint_01",""],"road_3":["joint_02"],"road_29":["joint5"], "road_7":["joint_03"], "road_10":["joint_04"], "road_12":["joint_05"], "road_15":["joint_06"], "road18":["joint_07"], "road_21":["joint_08"], "road_4":["joint_09"], "road_28":["joint3"], "edge_82":["joint6"], "edge_33":["joint4"]]
            
            if jointDictionariereThree.keys.contains(isInflectionPoint) {
              
                for  i in jointDictionariereThree[isInflectionPoint]! {
                        jointNode!.childNode(withName: i, recursively: true)?.isHidden = false
                        jointNode?.childNode(withName: i, recursively: true)?.geometry?.materials.first?.diffuse.contents = "Start.PNG"
                    
                   
                }

            }
           
           
                      
            if logicDictionariesThree.keys.contains(isInflectionPoint) {
                for i in 0..<logicDictionariesThree[isInflectionPoint]!.count {
                    node.childNode(withName: (logicDictionariesThree[isInflectionPoint]?[i])!, recursively: true)?.isHidden = true
                    
                    print("XXXXXXXXXXX____\((logicDictionariesThree[isInflectionPoint]?[i])!)_______XXXXXXXXXX")
                    
                }
               
                
               
                
            }
            
           
            
            if isInflectionPoint == "road_16" {
                selectWrongRoad = true
                
            }
            
            
        default:
            break
        }
    
    }
    
    func roadNameArry() {
        logicDictionariesOne = ["road_1":["edge_1","edge_101"],"road_2":["edge_2","edge_22"],"road_3":["edge_3","edge_33"],"road_4":["edge_4","edge_44"],"road_5":["edge_5","edge_55"],"road_6":["edge_6","edge_66"],"road_7":["edge_7","edge_77"],"road_8":["edge_8","edge_88"],"road_9":["edge_9","edge_99"],"road_10":["edge_10","edge_110"],"road_11":["edge_11","edge_111"],"road_12":["edge_12","edge_112"],"road_13":["edge_13","edge_113"],"road_14":["edge_14","edge_114"],"road_15":["edge_15","edge_115"],"road_16":["edge_16","edge_116"],"road_17":["edge_17","edge_117"],"road_18":["edge_18","edge_118"],"road_19":["edge_19","edge_119"],"road_20":["edge_20","edge_210"],"road_21":["edge_21","edge_211"],"road_22":["edge_22","edge_222"],"road_23":["edge_23","edge_233"],"road_24":["edge_24","edge_244"],"road_25":["edge_25","edge_255"],"road_26":["edge_26","edge_266"],"road_27":["edge_27","edge_277"],"road_28":["edge_28","edge_288"],"road_29":["edge_29","edge_299"],"road_30":["edge_30","edge_330"]]
        
        logicDictionariesThree = ["road_1":["edge_1","edge_101"],"road_2":["edge_2","edge_202"],"road_3":["edge_1","edge_3","road_28","edge_22"],"road_4":["edge_4","edge_44"],"road_5":["edge_5","road_29"],"road_6":["edge_6","edge_66","edge_4"],"road_7":["edge_7","edge_77"],"road_8":["edge_8","edge_88"],"road_9":["edge_7","edge_9","edge_333","edge_32","edge_33"],"road_10":["edge_10","edge_110"],"road_11":["edge_11","edge_111","edge_112"],"road_12":["edge_12","edge_112"],"road_13":["edge_13","edge_113"],"road_14":["edge_14","edge_114","edge_52"],"road_15":["edge_15","edge_115"],"road_16":["edge_16","edge_116"],"road_17":["edge_17","edge_117","edge_62"],"road_18":["edge_18","edge_118"],"road_19":["edge_19","edge_119"],"road_20":["edge_20","edge_210"],"road_21":["edge_21","edge_211","edge_722"],"road_22":["edge_22","edge_222"],"road_23":["edge_23","edge_233","edge_82","edge_822"],"road_24":["edge_24","edge_244"],"road_25":["edge_25","edge_255"],"road_26":["edge_26","edge_266"],"road_27":["edge_27","edge_277","edge_93"],"road_28":["edge_22","edge_3"],"road_29":["edge_44","edge_5"],"road_30":["edge_30","edge_330"],"edge_33":["edge_32","edge_77","road_9","edge_9"],"edge_333":["edge_9","edge_32","road_9","edge_33"]]
        
        
    }
    func Supplementpoint (pass: Int, nodeName: String) {
        
    }
}


